package org.example.java.io;

import org.apache.commons.lang.ArrayUtils;

import java.util.*;

public class RandomUtil {
  private static final Random RANDOM = new Random();

  private static final char[] CHARACTER_RANGE = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
  private static final char[] NUMBER_RANGE = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};

  /**
   * @param size   List size
   * @param length chars length
   */
  public static List<String> randomAlphabeticList(int size, int length) {
    Set<String> codeSet = new HashSet<String>();
    while (codeSet.size() < size) {
      codeSet.add(random(length, CHARACTER_RANGE));
    }

    List<String> codeList = new ArrayList<String>();
    codeList.addAll(codeSet);
    return codeList;
  }

  public static String randomAlphabetic(int length) {
    return random(length, CHARACTER_RANGE);
  }

  public static String randomNumber(int length) {
    return random(length, NUMBER_RANGE);
  }

  public static String random(int count, char[] chars) {
    if (count == 0) {
      return "";
    } else if (count < 0) {
      throw new IllegalArgumentException("requested random string length " + count + " is less than 0.");
    } else if (ArrayUtils.isEmpty(chars)) {
      throw new IllegalArgumentException("requested random chars is empty.");
    }
    char[] buffer = new char[count];
    int gap = chars.length;
    while (count-- != 0) {
      buffer[count] = chars[RANDOM.nextInt(gap)];
    }
    return new String(buffer).toLowerCase();
  }

  public static String[] random(int count, String[] strings) {
    if (count == 0) {
      return new String[0];
    } else if (count < 0) {
      throw new IllegalArgumentException("requested random string length " + count + " is less than 0.");
    } else if (ArrayUtils.isEmpty(strings)) {
      throw new IllegalArgumentException("requested random chars is empty.");
    }
    String[] buffer = new String[count];
    int gap = strings.length;
    while (count-- != 0) {
      buffer[count] = strings[RANDOM.nextInt(gap)];
    }
    return buffer;
  }

}
